<template>
  <div class="hwcreate-container">
    <div class="position">
      <span>当前位置：</span>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>数据查询</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/teacher/hwinfo' }"
          >作业情况查询</el-breadcrumb-item
        >
        <el-breadcrumb-item>作业布置</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="hwform">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="课程ID" prop="courseid" required>
          <el-input v-model="ruleForm.courseid"></el-input>
        </el-form-item>
        <el-form-item label="课程名称" prop="coursename" required>
          <el-input v-model="ruleForm.coursename"></el-input>
        </el-form-item>
        <el-form-item label="作业ID" prop="hwid" required>
          <el-input v-model="ruleForm.hwid"></el-input>
        </el-form-item>
        <el-form-item label="作业标题" prop="hwtitle" required>
          <el-input v-model="ruleForm.hwtitle"></el-input>
        </el-form-item>
        <el-form-item label="发布时间" style="text-align: left" required>
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="ruleForm.date1"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止时间" style="text-align: left" required>
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="ruleForm.date2"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作业要求" prop="desc">
          <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即布置</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import http from "@/api/teacherapi";
export default {
  name: "",
  data() {
    return {
      ruleForm: {
        courseid: "",
        coursename: "",
        date1: "",
        date2: "",
        desc: "",
        hwid: "",
        hwtitle: "",
        coursename: "",
      },
      rules: {
        hwid: [{ required: true, message: "请输入作业名", trigger: "blur" }],
        hwtitle: [
          { required: true, message: "请输入作业标题", trigger: "blur" },
        ],
        courseid: [
          { required: true, message: "请输入课程ID", trigger: "blur" },
          { min: 8, max: 8, message: "请输入8个数字", trigger: "blur" },
        ],
        coursename: [
          { required: true, message: "请输入课程名称", trigger: "blur" },
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        date2: [
          {
            type: "date",
            required: true,
            message: "请选择时间",
            trigger: "change",
          },
        ],
        desc: [{ required: true, message: "请填写作业要求", trigger: "blur" }],
      },
      username: "",
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          http.sethomework({
            username: this.username,
            courseid: this.ruleForm.courseid,
            coursename: this.ruleForm.coursename,
            hwid:this.ruleForm.hwid,
            hwtitle: this.ruleForm.hwtitle,
            posttime: this.ruleForm.date1,
            finaltime:this.ruleForm.date2,
            desc: this.ruleForm.desc 
          }).then((res)=>{
            if(res.status==200){
              this.$Toast({
                content: res.data.msg,
                type: res.data.type
              })
            }
          },(error)=>{return Promise.reject(new Error("获取服务器数据失败！"))})
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    this.username = localStorage.getItem("username");
  },
};
</script>

<style lang="less" scoped>
.position {
  height: 30px;
  line-height: 30px;
  .el-breadcrumb {
    display: inline-block;
  }
  & > span {
    font-size: 16px;
  }
}
.hwform {
  margin-top: 30px;
  .line {
    text-align: center;
  }
}
</style>
